﻿/************************************************************************
 * AIDE - Automata Identification Engine
 * Copyright (C) 2012-2013 Ali Khalili (khalili_DOT_ir_@SIGN_gmail_DOT_com)
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL) published
 * by the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version. This program is distributed without any
 * warranty. See the GNU Lesser General Public License for more details.
 * ***********************************************************************
 * File         :     AIDE\Base\LibSetting.cs
 * Description  :     LibSetting of Library
 * Change log   :     -1 Sep 2013:  Initial version
 * 					  -2 May 2014: change XML config file into txt files
 * To Do        :
 ************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;

/// <summary>
/// Some basic functionality for internal usage
/// </summary>
namespace AIDE.Base
{
	/// <summary>
	/// General configuration of the library
	/// <remarks>Before, I was using XML file, I changed it into txt file</remarks>
	/// </summary>
	public class LibSetting
	{
		static bool initialized = false;
	
		 #region -=[ CONFIGURATION ]=-
		public static double Confident;
		public static double MinPro;
		public static int DebuggingLevel = -1;

		/// <summary>
		/// Default working directory
		/// </summary>
		public static string WorkingPath = "";
		
		static void ExtractValues ()
		{
			WorkingPath = valOf ("AIDE.WorkingPath");
			Console.WriteLine (WorkingPath);

			//* NSTAR
			string confPro = valOf ("AIDE.NStar.CONF_PROB");
			double.TryParse (confPro, out Confident);

			Console.WriteLine (Confident);
		}
		#endregion
		
		/// <summary>
		/// Check weather it is initialized or not?
		/// </summary>
		public static bool IsInitialized {
			get {
				return initialized;
			}
		}


		/// <summary>
		/// Extracting the configuration value the of some property
		/// </summary>
		/// <returns>
		/// The config value
		/// </returns>
		/// <param name='prop'>
		/// The interesting property
		/// </param>
		public static string ConfOf (string prop)
		{
			if (!initialized) {
				Console.WriteLine ("Warning: AIDE config was not initialized!");
				Init ();
			}
			return valOf (prop);
		}

		static Dictionary<string,string> db;

		public static Dictionary<string,string> Knowledge {
			get {
				return db;
			}
		}

		static string valOf (string prop)
		{
			string val;
			db.TryGetValue (prop, out val);
			return val;
		}

		/// <summary>
		/// Init the specified db.
		/// </summary>
		/// <param name='db'>
		/// A dictionary to initialize the setting
		/// </param>
		public static void Init (Dictionary<string,string> db)
		{
			LibSetting.db = db;
			initialized = true;
			ExtractValues ();
		}

		

		/// <summary>
		/// Init the specified configFilePath.
		/// </summary>
		/// <param name='configFilePath'>
		/// Config file path.
		/// </param>
		public static void Init (string configFilePath)
		{
			try {
				readConfigFile (configFilePath);
			} catch {
				//* if there was an error!
				return;
			}

			ExtractValues ();
			initialized = true;
		}

		/// <summary>
		/// Reads the config file.
		/// </summary>
		/// <param name='fpath'>
		/// File path
		/// </param>
		static void readConfigFile (string fpath)
		{
			if (!System.IO.File.Exists (fpath)) {
				Console.Error.WriteLine ("Fatal error: Configuration file at \"" + fpath + "\" not found!");
				return;
			}

			db = new Dictionary<string, string> ();
			StreamReader sr = new StreamReader (fpath);
			string line;
			char[] stringSeparators = new char[] {'\t'};
			int i = 0;
			while ((line=sr.ReadLine())!=null) {
				i++;
				//* line is a comment?
				if (line.StartsWith ("#"))
					continue;
				string [] vals = line.Split (stringSeparators);
				if (vals.Length != 2) {
					Console.WriteLine ("Error in config file (line " + i + ")");
					continue;
				}
				db.Add (vals [0], vals [1]);
			}
			sr.Close ();
		}


		/// <summary>
		/// Initialize the library (from a text configuration file)
		/// </summary>
		public static void Init ()
		{
			string appDir = System.IO.Path.GetDirectoryName (System.Reflection.Assembly.GetEntryAssembly ().Location);
			string fpath = appDir + "/Config.txt";

			Init (fpath);
		}
	}
}
